package com.njlime.core.helper;


import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页辅助类
 * 
 * @author Dean
 * @since 2017年07月19日
 * @version 1.0
 */
@Component("pageBean")
public class PageBean {

    // 当前页列表数据
    private List<Map<String, Object>> MapList;
    //总条数
    private int totalCount;
    //当前页数
    private int curPage;
    // 总页数
    private int totalPage;
    // 每页显示多少条
    private int pageSize;


    /**
     * 重写无参构造函数：获取设置的默认分页行数
     *
     */
    public PageBean() {
        this.pageSize = 10;
        this.curPage = 1;
    }

    public PageBean(int pageSize) {
        this.pageSize = pageSize;
        this.curPage = 1;
    }

    /**
     * 转成map对象
     * @param pb
     * @return
     */
    public static Map<String,Object> transformListMap(PageBean pb){
        Map<String,Object> mapBean = new HashMap<>();
        mapBean.put("totalCount", pb.getTotalCount());
        mapBean.put("totalPage", pb.getTotalPage());
        mapBean.put("pageSize", pb.getPageSize());
        if (pb.getMapList() != null) {
            mapBean.put("pageCount", pb.getMapList().size());
        } else {
            mapBean.put("pageCount", 0);
        }
        mapBean.put("curPage", pb.getCurPage());
        mapBean.put("list", pb.getMapList());
        if(pb.getMapList() == null){
            mapBean.put("list", new ArrayList<Map<String,Object>>());
        }
        return mapBean;
    }

    public List<Map<String, Object>> getMapList() {
        return MapList;
    }

    public void setMapList(List<Map<String, Object>> mapList) {
        MapList = mapList;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public int getTotalPage() {
        totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

}
